//水印添加或修改.js

window.onload = function() {
	init_addImage();
	init_checkbox();
}

$(function() {
	$("body").on("click", ".del-c", function() {
		var productId = $(this).attr("id");
		deleteProduct(productId);
	});
});

$(".div_show").mouseover(
		function() {
			if ($(this).children("img").attr("src")
					&& $(this).children("img").attr("data-type") != "1") {
				$(this).find(".delete").show();
			}
		});
$(".div_show").mouseout(function() {
	$(this).find(".delete").hide();
});
$(".delete").click(function() {
	var fileinput = document.getElementById($(this).attr("data-id"));
	var img = $(this).parent().prev("img");
	var type = $(img).attr("data-type");
	layer.confirm("确定清空已选中的图片?", function() {
		layer.closeAll('dialog');
		if (type == 2) {
			img.removeAttr("src");
			img.removeAttr("data-type");
		} else if (type == 3) {
			img.attr("src", img.attr("data-oldsrc"));
			img.attr("data-type", "1");
		}
		init_checkbox();
		fileinput.outerHTML = fileinput.outerHTML;
		init_addImage();
	});
});
function init_addImage() {
	/* 缩略图 */
	$(".addImage").change(function() {
		if (typeof (FileReader) != "undefined") {
			var dvPreview = $("#" + $(this).attr("data-id"));
			var regex = /(.png)$/;
			$($(this)[0].files).each(function() {
				var file = $(this);
				if (regex.test(file[0].name.toLowerCase())) {
					var reader = new FileReader();
					reader.onload = function(e) {
						var img = dvPreview.children("img");
						if (img.attr("src") && img.attr("data-type") == '1') {
							img.attr("data-oldsrc", img.attr("src"));
							img.attr("data-type", "3");
						} else {
							img.attr("data-type", "2");
						}
						img.attr("src", e.target.result);
						img.css({
							height : dvPreview.css("height"),
							width : dvPreview.css("width")
						});
						init_checkbox();
					}
					reader.readAsDataURL(file[0]);
				} else {
					layer.alert(file[0].name + "不是png文件!");
					var fileinput = document.getElementById(dvPreview.find('.delete').attr('data-id'));
					fileinput.outerHTML = fileinput.outerHTML;
					init_addImage();
					return false;
				}
			});
		} else {
			layer.alert("抱歉!浏览器版本太低了!不支持预览图片,换个浏览器试试吧~");
		}
	});
}
function toAddProduct() {
	var url = path + "/product_water_mark/to_add_product";
	var index = layer.open({
		type : 2,
		title : "添加商品",
		scrollbar : false,
		content : url,
		area : [ '800px', '600px' ]
	});
	layer.full(index);
}

// 添加选中的商品ID
function addProduct(productId) {
	var productIds = document.getElementById("saleProducIds").value;
	var productIdArr = productIds.split(",");
	var exist = false;
	for (var i = 0; i < productIdArr.length; i++) {
		if (productIdArr[i] == productId) {
			exist = true;
			break;
		}
	}
	if (!exist) {
		if (productIds.length == 0) {
			productIds = productIds + productId + ",";
		} else {
			productIds = productIds + productId + ",";
		}
	}
	document.getElementById("saleProducIds").value = productIds;

	$("#saleProductLength").val(productIdArr.length);
}

// 删除商品ID
function deleteProduct(productId) {
	var productIds = document.getElementById("saleProducIds").value;
	var productIdArr = productIds.split(",");
	var result = "";
	for (var i = 0; i < productIdArr.length; i++) {
		if (productIdArr[i] != productId) {
			if (productIdArr.length - 1 == i) {
				result = result + productIdArr[i];
			} else {
				result = result + productIdArr[i] + ",";
			}
		}
	}
	document.getElementById("saleProducIds").value = result;

	$("#sale" + productId).remove();
	var saleLength = $("#saleProductLength").val();
	saleLength--;

	$("#saleProductLength").val(saleLength);
	$("#product_id").html("您已选择" + saleLength + "款商品");
}

function sub_form() {
	// 判断必填项
	// 水印名称
	var syName = $("input[name='name']").val();
	syName = trim(syName);
	if (syName == null || syName == "") {
		layer.alert("请填写水印名称");
		return false;
	}
	// 水印图片
	var addMobWmPic = $("#addMobWmPic").val();
	var mainPicture = $("#showMobWmPic").children("img");
	//如果没有文件
	if ((addMobWmPic.length <= 0 && mainPicture.attr("data-type") != "1")
			|| mainPicture.attr("src").length<=0) {
		layer.alert("请上传水印图片");
		return false;
	}

	// 显示位置
	var str = document.getElementsByName("position");
	var objarray = str.length;
	var position = "";
	for (i = 0; i < objarray; i++) {
		if (str[i].checked) {
			position = str[i].value;
			break;
		}
	}
	if (position == "") {
		layer.alert("请选择水印位置");
		return false;
	}
	// 开始时间
	var syStart = $("input[name='startDate']").val();
	syStart = trim(syStart);
	if (syStart == null || syStart == "") {
		layer.alert("请选择开始时间");
		return false;
	}
	// 结束时间
	var syEnd = $("input[name='endDate']").val();
	syEnd = trim(syEnd);
	if (syEnd == null || syEnd == "") {
		layer.alert("请选择结束时间");
		return false;
	}
	if (syStart != "" && syEnd != "") {
		var startDate = new Date(syStart);
		var endDate = new Date(syEnd);
		if (startDate > endDate) {
			layer.alert("开始时间不能大于结束时间！");
			return false;
		}
	}

	// 商品
	var length = $("#saleProductLength").val();
	var ids = document.getElementById("saleProducIds").value;
	if (trim(ids) == "" || trim(ids) == "," || length <= 0) {
		layer.alert("请选择添加的商品");
		return false;
	}
	var layer_load_window = layer.load(2, {
		time : 10 * 1000
	});
	$('#save_form').submit();
}
function callback(success, message) {
	layer.closeAll('loading');
	if (success == true) {
		layer.msg('操作成功!', {
			icon : 1,
			time : 1000
		}, function() {
			var index = parent.layer.getFrameIndex(window.name);
			parent.search();
			parent.layer.close(index);
		});
	} else {
		layer.alert(message);
	}
}
function init_checkbox() {
	$("input[name=position]:checked").click();
}
// checkbox点击
function leftTop() {
	var url = $('#showMobWmPic img').attr("src");
	if (url != undefined) {
		var html = "<div style=\"position: absolute;z-indent:2;left:5px;top:5px\"><img src=\""
				+ url + "\"/>";
		document.getElementById("preShowPic").innerHTML = html;
	} else {
		document.getElementById("preShowPic").innerHTML = "";
	}
}

function rightTop() {
	var url = $('#showMobWmPic img').attr("src");
	if (url != undefined) {
		var html = "<div style=\"position: absolute;z-indent:2;left:100px;top:5px\"><img src=\""
				+ url + "\"/>";
		document.getElementById("preShowPic").innerHTML = html;
	} else {
		document.getElementById("preShowPic").innerHTML = "";
	}
}

function leftBottom() {
	var url = $('#showMobWmPic img').attr("src");
	if (url != undefined) {
		var html = "<div style=\"position: absolute;z-indent:2;left:5px;top:154px\"><img src=\""
				+ url + "\"/>";
		document.getElementById("preShowPic").innerHTML = html;
	} else {
		document.getElementById("preShowPic").innerHTML = "";
	}
}

function rightBottom() {
	var url = $('#showMobWmPic img').attr("src");
	if (url != undefined) {
		var html = "<div style=\"position: absolute;z-indent:2;left:100px;top:154px\"><img src=\""
				+ url + "\"/>";
		document.getElementById("preShowPic").innerHTML = html;
	} else {
		document.getElementById("preShowPic").innerHTML = "";
	}
}
// js去掉空格
function trim(str) {
	return str.replace(/^\s+|\s+$/g, "");
}
function cancle() {
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
}